package com.example.aiquestions.service.impl;

import com.example.aiquestions.entity.QuestionLibrary;
import com.example.aiquestions.mapper.QuestionLibraryMapper;
import com.example.aiquestions.model.dto.QuestionDetail;
import com.example.aiquestions.service.QuestionLibraryService;
import com.example.aiquestions.service.QuestionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service
public class QuestionLibraryServiceImpl implements QuestionLibraryService {

    @Autowired
    private QuestionLibraryMapper libraryMapper;

    @Autowired
    private QuestionService questionService;

    @Override
    public PageInfo<QuestionLibrary> getLibraryListByUserAndSubject(Long userId, Long subjectId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<QuestionLibrary> list = libraryMapper.getLibraryListByUserAndSubject(userId, subjectId);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<QuestionLibrary> getLibraryListBySubject(Long subjectId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<QuestionLibrary> list = libraryMapper.getLibraryListBySubject(subjectId);
        return new PageInfo<>(list);
    }

    @Override
    @Transactional
    public QuestionLibrary createLibrary(Long userId, Long subjectId, String name, List<Long> questionIds) {
        // 创建题库
        QuestionLibrary library = new QuestionLibrary();
        library.setId(UUID.randomUUID().toString());
        library.setName(name);
        library.setQuestionCount(questionIds.size());
        library.setCreatedId(userId);
        library.setSubjectId(subjectId);
        library.setCreatedAt(new java.util.Date());
        library.setUpdatedAt(new java.util.Date());
        library.setStatus(1);
        libraryMapper.createLibrary(library);

        // 批量插入题库-题目关联
        if (!questionIds.isEmpty()) {
            libraryMapper.batchInsertLibraryQuestions(library.getId(), questionIds);
        }

        return library;
    }

    @Override
    @Transactional
    public QuestionLibrary updateLibrary(String id, String name, Long subjectId) {
        // 更新题库
        QuestionLibrary library = getLibraryById(id);
        library.setName(name);
        library.setSubjectId(subjectId);
        library.setUpdatedAt(new java.util.Date());
        libraryMapper.updateLibrary(library);

        // 返回更新后的题库信息
        return library;
    }

    @Override
    @Transactional
    public boolean deleteLibrary(String id) {
        return libraryMapper.deleteLibrary(id) > 0;
    }

    @Override
    @Transactional
    public QuestionLibrary addQuestionsToLibrary(String libraryId, List<Long> questionIds) {
        // 批量插入题库-题目关联
        if (!questionIds.isEmpty()) {
            libraryMapper.batchInsertLibraryQuestions(libraryId, questionIds);
        }

        // 更新题目数量
        libraryMapper.updateQuestionCount(libraryId);

        // 返回更新后的题库信息
        return getLibraryById(libraryId);
    }

    @Override
    @Transactional
    public QuestionLibrary deleteQuestionsFromLibrary(String libraryId, List<Long> questionIds) {
        // 删除题库-题目关联
        if (!questionIds.isEmpty()) {
            libraryMapper.deleteQuestions(libraryId, questionIds);
        }

        // 更新题目数量
        libraryMapper.updateQuestionCount(libraryId);

        // 返回更新后的题库信息
        return getLibraryById(libraryId);
    }

    @Override
    public List<QuestionDetail> getLibraryQuestions(String libraryId) {
        // 获取题库中的题目ID列表
        List<Long> questionIds = libraryMapper.getLibraryQuestionIds(libraryId);
        
        // 查询题目详细信息
        if (!questionIds.isEmpty()) {
            return questionService.getQuestionsByIds(questionIds);
        }
        return List.of();
    }

    @Override
    public QuestionLibrary getLibraryById(String id) {
        return libraryMapper.getLibraryById(id);
    }
}
